/*
  Copyright (c) 2014 Dimitrij Gester

  "Smart RC TX" is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  "Smart RC TX" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with "Smart RC TX"; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
*/

package com.codeplex.smartrctx.configurator.main.controller;

import java.util.Observable;

import com.codeplex.smartrctx.configurator.main.model.RCSettings;

import android.widget.TextView;

public class CompoundMixerSetter extends BaseSetter {
	private int src = 0;
	private int dst = 0;
	private int valueSelector = 0;

	public CompoundMixerSetter(TextView v){
		super(v, 0);
	}
	
	public void setSource(int s) {
		src = s;
	}

	public void setDestination(int d) {
		dst = d;
	}
	
	public void setValueSelector(int v) {
		if (v < 0 || v > 1) 
			throw new IllegalArgumentException();
		
		valueSelector = v;
	}

	@Override
	public void setValue(String v) {
		Integer[][][] data = RCSettings.getSettings().mixer; 
		data[src][dst][valueSelector] = null;
		try {
			data[src][dst][valueSelector] = Integer.valueOf(v);
		} catch (NumberFormatException e) {
		}
	}

	@Override
	public void updateView() {
		// TODO: make sure that the fragment's src and dst spinners point to
		// the same data that we do, even in the event of killing and resuming the activity
		Integer[][][] data = RCSettings.getSettings().mixer;
		
		if(target != null) {
			String t = "";
			if(data[src][dst][valueSelector] != null) {
				t = data[src][dst][valueSelector].toString();
			}
			target.setText(t);
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		updateView();
	}

	protected Integer[] getData() {
		return null;
	}

}
